package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.UserLike;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        //判断喜欢的关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        if (mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }
        UserLike userLike = new UserLike(ObjectId.get(), userId, likeUserId, System.currentTimeMillis());
        UserLike save = mongoTemplate.save(userLike);
        return save.getId().toHexString();
    }

    /**
     * 是否互相喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, UserLike.class);
        return count == 2;
    }

    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        DeleteResult remove = mongoTemplate.remove(query, UserLike.class);
        return remove.getDeletedCount() == 1;
    }

    @Override
    public Long queryEachLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if (CollectionUtils.isEmpty(userLikes)) {
            return 0L;
        }
        Set<Long> likeUserIds = new HashSet<>();
        for (UserLike userLike : userLikes) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        Query query1 = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));
        return mongoTemplate.count(query1, UserLike.class);
    }

    @Override
    public Long queryLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageInfo<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike> pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        if (CollectionUtils.isEmpty(userLikes)){
            return pageInfo;
        }
        Set<Long> likeUserIds = new HashSet<>();
        for (UserLike userLike : userLikes) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        PageRequest pageRequest = PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query1 = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));
        query1.with(pageRequest);
        List<UserLike> userLikeList = mongoTemplate.find(query1, UserLike.class);
        pageInfo.setRecords(userLikeList);
        return pageInfo;
    }

    @Override
    public PageInfo<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId));
        query.with(pageRequest);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike> pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(userLikes);
        return pageInfo;
    }

    @Override
    public PageInfo<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        query.with(pageRequest);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike> pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(userLikes);
        return pageInfo;
    }
}
